<template>
	<div class="m-view-fileSignInfo m-window">
		<el-row :gutter="10">
			<el-col :span="3" v-if="btn_download_show" style="float:right">
				<el-button type="primary" @click="downLoadFile">下载</el-button>
			</el-col>
			<el-col :span="3" v-if="btn_sign_show && fileSignIn.signMode == 0" style="float:right">
				<el-button type="primary" @click="sign">签署</el-button>
			</el-col>
			<el-col :span="3" v-if="btn_cancel2_show" style="float:right">
				<el-button @click="cancel2">返回</el-button>
			</el-col>
		</el-row>
		<div class="preview-col" v-loading="fileLoading">
			<iframe :src="fileSrc" height="100%" width="100%" frameborder="0"></iframe>
		</div>
	</div>
</template>

<style lang="less" scoped>
.m-window {
	.preview-col {
		margin-top: 20px;
		height: 100%;
		width: 100%;
	}
}
</style>

<script>
import _Api from "@/api";
import apienv from "@/api/env";
import localeText from "@/i18n/agGridText";
import constant from "@/public/constant";
import Msg from "@/public/message.js";
import downLoadFileFunc from "@/public/downLoadFile";
import _utils from "@/public/utils";

export default {
	data () {
		return {
			gD_account: [],
			gD_account_columnDefs: [],
			gD_account_rowData: [],
			gD_account_loading: false,
			sup_info: "", //供应商盖章区
			com_info: "", //公司盖章区
			btn_cancel2_show: true,
			btn_sign_show: false,
			btn_look_show: true,
			btn_download_show: false,
			supplierInfo: "",
			token: "",
			doc_id: "",
			remoteUrl: apienv[constant.appenv].esign + "SSOSign/SignLogin",
			paramsStr: "?param=",
			dialogVisible: false,
			title: "对账函",
			tD_account: [],
			sup_name: "",
			unConMemo: "",
			com_name: "",
			sDate: "", //开始日期
			eDate: "", //截止日期
			createDate: "",
			initCharge: "", //期初
			handleCharge: "", //应付
			alreadyCharge: "", //已付
			reconcyAmount: "", //余额
			contract_code: "",
			accord_show: true,
			inconformity_show: true,
			testTest: false,
			fileLoading: false,
			fileSrc: "",
		};
	},
	beforeMount () {
		//对账款项-表格属性
		this.gD_account = {
			context: {
				componentParent: this,
			},
			rowSelection: "multiple",
			enableColResize: true,
			localeText: localeText,
			suppressRowClickSelection: true,
		};
	},
	methods: {
		getBtnAuth () {
			var that = this;
			var obj = {
				code: "filesSignList",
				resultType: "0",
				authClientCode: constant.authCode.code,
				authTenantCode: constant.authCode.tcode
			};
			_Api.getUserAuthMenu(obj).then(function (response) {
				let data = response.data;
				if (data.code == "1") {
					if (data.total > 0) {
						for (let i in data.rows) {
							if (data.rows[i].code == "sign" && that.fileSignIn.signState < 3)
								that.btn_sign_show = true;
							else that.btn_sign_show = false;
						}
					}
				}
			});
		},
		cancel2: function () {
			this.$emit("cancelComponents", true);
		},
		//签署
		sign () {
			if (this.$global.user.userTypeId == 4) {
				this.doc_id = this.fileSignIn.docId;
				this.getToken(7);
			} else {
				this.doc_id = this.fileSignIn.docId;
				this.getToken(7);
			}
		},
		//获取用户的token
		getToken (isLook) {
			var obj2 = {};
			this.fileLoading = true;

			if (this.$global.user.userTypeId == 4) {
				//供应商电子签帐号
				obj2.name = this.fileSignIn.seSignAccount;
				// if (constant.appenv == "app")
				//   obj2.name = this.fileSignIn.seSignAccount;
				// else if (constant.appenv == "qas")
				//   obj2.name = this.fileSignIn.seSignAccount;
				// else
				//   obj2.name = "lanshan";
			}
			else {
				//公司电子签帐号
				obj2.name = this.fileSignIn.ceSignAccount;
				// if (constant.appenv == "app")
				//   obj2.name = this.fileSignIn.ceSignAccount;
				// else if (constant.appenv == "qas")
				//   obj2.name = this.fileSignIn.ceSignAccount;
				// else
				//   obj2.name = "yuanfeng";
			}
			var that = this;

			_Api
				.getESignToken(obj2)
				.then(function (response) {
					var data_token = response.data;
					if (data_token.Code == 0) {
						that.fileLoading = false;
						that.token = data_token.Data;
						if (isLook === 7) {
							that.SSOLogin();
						} else {
							if (constant.appenv == "app") {
								that.fileSrc = `https://esign.lesso.com/esign/SSOSign/SignLogin?param=${that.token}&url=https://esign.lesso.com/Esign/Sign/indexView?doc_id=${that.fileSignIn.docId}`;
							} else {
								that.fileSrc = `https://esign.lesso.com/testesign/SSOSign/SignLogin?param=${that.token}&url=https://esign.lesso.com/TestEsign/Sign/indexView?doc_id=${that.fileSignIn.docId}`;
							}
						}
					} else {
						that.fileLoading = false;
						Msg.showErrorMsg(data_token.info);
					}
				})
				.catch((err) => {
					that.fileLoading = false;
					Msg.showErrorMsg("请求失败！");
				});
		},
		//弹出电子签系统，并单点登录进去
		SSOLogin () {
			this.webviewUrl =
				this.remoteUrl +
				this.paramsStr +
				this.token +
				"&url=" +
				apienv[constant.appenv].esign +
				"Sign/Index?doc_id=" +
				this.doc_id;
			if (constant.userAgent == "web") {
				_utils.openNewWindow(this.webviewUrl);
			} else {
				shell.openExternal(this.webviewUrl);
			}
			this.cancel2();
		},
		//浏览
		look () {
			this.dialogVisible = true;
		},
		//下载
		downLoadFile () {
			var group = "";
			// var viewURL = this.fileSignIn.url;
			if (this.fileSignIn.url.indexOf("SRM") > -1) {
				group = "SRM";
			} else if (this.fileSignIn.url.indexOf("SIGN") > -1) {
				group = "SIGN";
			} else if (this.fileSignIn.url.indexOf("TEST") > -1) {
				group = "TEST";
			} else if (this.fileSignIn.url.indexOf("MES") > -1) {
				group = "MES";
			}
			let splitUrl = this.fileSignIn.url.split(".");
			let fileType = splitUrl[splitUrl.length - 1];
			var obj = {
				viewPath: `${group}${this.fileSignIn.url.split(group)[1]}`,
				groupName: group,
				fileName: "",
			};
			downLoadFileFunc(obj, this.fileSignIn.accountNo + "." + fileType);
			// var obj = [{
			//   accountNo : this.fileSignIn.accountNo,
			//   supplierName : this.fileSignIn.supplierName,
			//   fileResult:{
			//     groupName:group,
			//     viewPath:viewURL
			//   }
			// }];
			//   var that = this;
			//   _Api.downLoadFile(obj).then(function (response) {
			//   // _Api.totalDownload(obj).then(function (response) {
			//     var data = response.data;
			//     if (data.code == "1") {
			//       if (data.total > 0) {
			//         that.downLoad(that.fileSignIn.accountNo + ".pdf", data.rows[0].fileName);//data.rows[0].filename
			//       }else {
			//           Msg.showErrorMsg(data.info);
			//           return;
			//       }
			//     }
			//     // if(that.$global.user.userTypeId == 4){
			//     //     that.downLoad(constant.employeeName + '.zip', data);
			// 			// 	}else if(that.$global.user.userTypeId == 1){
			// 			// 		that.downLoad(constant.butxt + '.zip', data);
			// 			// 	}
			//   }).catch(err=>{
			//        Msg.showErrorMsg(err.data.message);
			//        return;
			//   })
		},
		downLoad (filename, url) {
			// let blob = new Blob([url],{
			//   type:'application/octet-stream'
			// })
			// let blobURL = window.URL.createObjectURL(blob);
			var eleLink = document.createElement("a");
			eleLink.style.display = "none";
			eleLink.setAttribute("href", url); //文件下载地址
			// eleLink.setAttribute("href", blobURL);   //文件下载地址
			eleLink.setAttribute("download", filename); //download属性只能在同域实现
			// 触发点击
			document.body.appendChild(eleLink);
			eleLink.click();
			// 然后移除
			document.body.removeChild(eleLink);
		},
		handleClose (done) {
			done();
		},
	},
	created () {
		const { signMode, url } = this.fileSignIn;
		if (signMode == 0) {
			this.getToken();
		} else {
			let fileurl = url.split("SRM");
			let path = `SRM${fileurl[1]}`;
			let pramas = {
				groupName: "SRM",
				viewPath: path,
			};
			this.$http.fastDfsDownLoad(pramas).then(({ data }) => {
				this.fileSrc = data.data;
			});
		}
	},
	mounted () {
		//根据单据状态，显示下载
		//双方签署完后，才能下载
		if (this.fileSignIn.signState == "1") this.btn_download_show = false;
		else if (this.fileSignIn.signState == "2") this.btn_download_show = false;
		else if (this.fileSignIn.signState == "3" && this.fileSignIn.url != "") {
			this.btn_download_show = true;
		}
		//供应商类型用户不限制签署按钮
		if (constant.accountType == 4) {
			if (this.fileSignIn.signState == "3") {
				this.btn_sign_show = false;
			} else {
				this.btn_sign_show = true;
			}
		} else {
			//通过权限控制按钮是否显示
			this.getBtnAuth();
		}
	},
	props: {
		removeTab: {
			type: Function,
		},
		fileSignIn: {
			type: Object,
		},
	},
	components: {},
};
</script>
